package com.gang.ssm.bos.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gang.ssm.bos.common.ShiroUser;
import com.gang.ssm.bos.core.constant.SessionAttr;
import com.gang.ssm.bos.core.toolbox.ajax.AjaxResult;
import com.gang.ssm.bos.system.bean.Workordermanage;
import com.gang.ssm.bos.system.service.WorkordermanageService;

/**
 * 任务
 * @author Administrator
 *
 */

@Controller
@RequestMapping("/task")
public class TaskController extends BosController {
	
	@Autowired
	private TaskService taskService;
	
	@Autowired
	private RuntimeService runtimeService;
	
	@Autowired
	private WorkordermanageService workordermanageService;
	
	@RequestMapping("/findGroupTask")
	public String findGroupTask(HttpSession session,Map<String, Object> map) {
		TaskQuery taskQuery = taskService.createTaskQuery();
		ShiroUser user = (ShiroUser)session.getAttribute(SessionAttr.USER_LOGIN.getValue());
		String candidateUser = user.getId().toString();
		taskQuery.taskCandidateUser(candidateUser);
		List<Task> list = taskQuery.list();
		
		map.put("list", list);
		
		Map<String, String> dataMap = new HashMap<String, String>();
		for (Task task : list) {
			dataMap.put(task.getId(), taskService.getVariables(task.getId()).toString());
		}
		map.put("dataMap", dataMap);
		
		return "system/workflow/grouptask";
	}
	
	@ResponseBody
	@RequestMapping("/takeTask/{taskId}")
	public AjaxResult takeTask(@PathVariable("taskId")String taskId, HttpSession session) {
		try {
			ShiroUser user = (ShiroUser) session.getAttribute(SessionAttr.USER_LOGIN.getValue());
			taskService.claim(taskId, user.getId().toString());
			
			return success("拾取任务成功!");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail("拾取任务失败!");
	}
	
	@RequestMapping("/findPersonalTask")
	public String findPersonalTask(HttpSession session,Map<String, Object> map) {
		ShiroUser user = (ShiroUser) session.getAttribute(SessionAttr.USER_LOGIN.getValue());
		String candidateUser = user.getId().toString();
		
		TaskQuery taskQuery = taskService.createTaskQuery();
		taskQuery.taskAssignee(candidateUser);
		List<Task> list = taskQuery.list();
		map.put("list", list);
		
		Map<String, String> dataMap = new HashMap<String, String>();
		for (Task task : list) {
			dataMap.put(task.getId(), taskService.getVariables(task.getId()).toString());
		}
		map.put("dataMao", dataMap);
		
		return "system/workflow/personaltaskList";
	}
	
	@ResponseBody
	@RequestMapping("/checkWorkOrderManage/{taskId}")
	public AjaxResult checkWorkOrderManage(@PathVariable("taskId")String taskId, Map<String, Object> map,
				@RequestParam(value="check", required=false) Integer check) {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		String processInstanceId = task.getProcessInstanceId();
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
		String workordermanageId = processInstance.getBusinessKey();
		Workordermanage workordermanage = workordermanageService.findById(Integer.valueOf(workordermanageId));
		
		if(check == null) {
			map.put("map", workordermanage);
			map.put("taskId", taskId);
		}else{
			workordermanageService.checkWorkordermanage(taskId,check,workordermanageId);
			return success();
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping("/outStore/{taskId}")
	public AjaxResult outStore(@PathVariable("taskId") String taskId) {
		try {
			taskService.complete(taskId);
			
			return success();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail();
	}
	
	@ResponseBody
	@RequestMapping("/transferGoods/{taskId}")
	public AjaxResult transferGoods(@PathVariable("taskId") String taskId) {
		try {
			taskService.complete(taskId);
			
			return success();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail();
	}
	
	@ResponseBody
	@RequestMapping("/receive/{taskId}")
	public AjaxResult receive(@PathVariable("taskId") String taskId) {
		try {
			taskService.complete(taskId);
			
			return success();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fail();
	}

}
